﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace Netfraction
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        protected Color MessageTextColour;
        protected Color MyTextColour;
        protected Color NickTextColour;
        protected Color MyNickTextColour;
        protected Color OperatorNickTextColour;
        protected Color UrlTextColour;
        protected Color TimestampTextColour;

        public SettingsWindow()
        {
            InitializeComponent();

            
            Assembly curAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            string curCompany = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), MainWindow.GetAssemblyCompany(curAssembly));
            string ApplicationDataDir = System.IO.Path.Combine(curCompany, MainWindow.GetProductName(curAssembly));

            if (!System.IO.File.Exists(System.IO.Path.Combine(ApplicationDataDir, "Client.xml")))
                ClientSettings.CreateSettings(System.IO.Path.Combine(ApplicationDataDir, "Client.xml"));

            LoadPersonalInfo();
            LoadConnection();
            LoadDownloads();
            LoadColours();
            TimestampFormatTextBox.Text = ClientSettings.Appearance.TimeStamp.Format;
        }

        private void LoadPersonalInfo()
        {
            IdentityNameTextBox.Text = ClientSettings.Identity.Name;
            IdentityEmailTextBox.Text = ClientSettings.Identity.Email;
            IdentityDescriptionTextBox.Text = ClientSettings.Identity.Description;
            foreach (ComboBoxItem item in IdentityConnectionSpeedComboBox.Items)
            {
                if (item.Content as string == ClientSettings.Identity.Connection)
                {
                    IdentityConnectionSpeedComboBox.SelectedItem = item;
                }
            }
        }

        private void LoadConnection()
        {
            HubAddressTextBox.Text = ClientSettings.Connection.HubAddress;
            GatewayIPTextBox.Text = ClientSettings.Connection.GatewayIP;
        }

        private void LoadDownloads()
        {
            DownloadFolderTextBox.Text = ClientSettings.Downloads.DownloadFolder;
        }

        private void LoadColours()
        {
            MessageTextColour = ClientSettings.Appearance.MessageText.Colour;
            MyTextColour = ClientSettings.Appearance.MyText.Colour;
            NickTextColour = ClientSettings.Appearance.Nick.Colour;
            MyNickTextColour = ClientSettings.Appearance.MyNick.Colour;
            OperatorNickTextColour = ClientSettings.Appearance.OperatorNick.Colour;
            UrlTextColour = ClientSettings.Appearance.UrlText.Colour;
            TimestampTextColour = ClientSettings.Appearance.TimeStamp.Colour;

            MessageTextColourRect.Fill = new SolidColorBrush(MessageTextColour);
            MyTextColourRect.Fill = new SolidColorBrush(MyTextColour);
            NickTextColourRect.Fill = new SolidColorBrush(NickTextColour);
            MyNickTextColourRect.Fill = new SolidColorBrush(MyNickTextColour);
            OperatorNickTextColourRect.Fill = new SolidColorBrush(OperatorNickTextColour);
            UrlTextColourRect.Fill = new SolidColorBrush(UrlTextColour);
            TimestampTextColourRect.Fill = new SolidColorBrush(TimestampTextColour);
        }

        private void SetColours()
        {
            ClientSettings.Appearance.MessageText.Colour = MessageTextColour;
            ClientSettings.Appearance.MyText.Colour = MyTextColour;
            ClientSettings.Appearance.Nick.Colour = NickTextColour;
            ClientSettings.Appearance.MyNick.Colour = MyNickTextColour;
            ClientSettings.Appearance.OperatorNick.Colour = OperatorNickTextColour;
            ClientSettings.Appearance.UrlText.Colour = UrlTextColour;
            ClientSettings.Appearance.TimeStamp.Colour = TimestampTextColour;
        }

        private void BrowseFolderTextBox_Click(object sender, RoutedEventArgs e)
        {
        }

        private void SelectColourButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.CustomControls.ColorPickerDialog cPicker = new Microsoft.CustomControls.ColorPickerDialog();
            cPicker.StartingColor = GetCurrentSelectedColor(sender as Button);
            cPicker.Owner = this;

            bool? dialogResult = cPicker.ShowDialog();
            if (dialogResult != null && (bool)dialogResult == true)
            {
                SetCurrentSelectedColor(sender as Button, cPicker.SelectedColor);
            }
        }

        private Color GetCurrentSelectedColor(Button sender)
        {
            switch ((sender as Button).Name)
            {
                case "MessageTextColourButton":
                    return MessageTextColour;
                case "MyTextColourButton":
                    return MyTextColour;
                case "NickTextColourButton":
                    return NickTextColour;
                case "MyNickTextColourButton":
                    return MyNickTextColour;
                case "OperatorNickTextColourButton":
                    return OperatorNickTextColour;
                case "UrlTextColourButton":
                    return UrlTextColour;
                case "TimestampTextColourButton":
                    return TimestampTextColour;
                default:
                    return Colors.White;
            }
        }

        private void SetCurrentSelectedColor(Button sender, Color colour)
        {
            switch (sender.Name)
            {
                case "MessageTextColourButton":
                    MessageTextColour = colour;
                    MessageTextColourRect.Fill = new SolidColorBrush(colour);
                    break;
                case "MyTextColourButton":
                    MyTextColour = colour;
                    MyTextColourRect.Fill = new SolidColorBrush(colour);
                    break;
                case "NickTextColourButton":
                    NickTextColour = colour;
                    NickTextColourRect.Fill = new SolidColorBrush(colour);
                    break;
                case "MyNickTextColourButton":
                    MyNickTextColour = colour;
                    MyNickTextColourRect.Fill = new SolidColorBrush(colour);
                    break;
                case "OperatorNickTextColourButton":
                    OperatorNickTextColour = colour;
                    OperatorNickTextColourRect.Fill = new SolidColorBrush(colour);
                    break;
                case "UrlTextColourButton":
                    UrlTextColour = colour;
                    UrlTextColourRect.Fill = new SolidColorBrush(colour);
                    break;
                case "TimestampTextColourButton":
                    TimestampTextColour = colour;
                    TimestampTextColourRect.Fill = new SolidColorBrush(colour);
                    break;
                default:
                    break;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SetPersonalInfo();
            SetConnection();
            SetDownloads();
            SetColours();
            ClientSettings.Appearance.TimeStamp.Format = TimestampFormatTextBox.Text;

            ClientSettings.SaveSettings();
            this.Close();
        }

        private void SetPersonalInfo()
        {
            ClientSettings.Identity.Name = IdentityNameTextBox.Text;
            ClientSettings.Identity.Email = IdentityEmailTextBox.Text;
            ClientSettings.Identity.Description = IdentityDescriptionTextBox.Text;
            ClientSettings.Identity.Connection = (string)(IdentityConnectionSpeedComboBox.SelectedItem as ComboBoxItem).Content;
        }

        private void SetConnection()
        {
            ClientSettings.Connection.HubAddress = HubAddressTextBox.Text;
            ClientSettings.Connection.GatewayIP = GatewayIPTextBox.Text;
        }

        private void SetDownloads()
        {
            ClientSettings.Downloads.DownloadFolder = DownloadFolderTextBox.Text;
        }
    }
}
